package com.oocl.im.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;

import com.oocl.im.dao.UserDao;
import com.oocl.im.dao.impl.UserDaoImpl;
import com.oocl.im.pojo.MsgPackage;
import com.oocl.im.pojo.Operation;
import com.oocl.im.pojo.User;
import com.oocl.im.util.JsonUtil;

public class LoginHandler implements Runnable {
	private Socket socket;
	private BufferedReader reader;
	private PrintWriter writer;
	private HashMap<String, ChatHandler> handlerPool;
	private ExecutorService threadPool;

	public LoginHandler() {
		super();
	}

	public LoginHandler(Socket socket,
			HashMap<String, ChatHandler> handlerPool, ExecutorService threadPool) {
		super();
		this.socket = socket;
		this.handlerPool = handlerPool;
		this.threadPool = threadPool;
		try {
			reader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			writer = new PrintWriter(socket.getOutputStream(), true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// validate login info and dispatch chatHandler.
	@Override
	public void run() {
		// the json string of msgPackage
		String msgJson = null;
		try {
			msgJson = reader.readLine();
		} catch (IOException e1) {
			e1.printStackTrace();
			// can not read json,later work make no sense.
			return;
		}
		MsgPackage msgPackage = JsonUtil.stringToObject(msgJson);
		String loginner = msgPackage.getSender();
		String password = msgPackage.getContent();
		int errorFlag = 0;
		UserDao userDao = new UserDaoImpl();
		User user = userDao.findByName(loginner);
		if (user == null || !password.equals(user.getPassword()))
			errorFlag = 1;
		else if (handlerPool.containsKey(loginner)
				&& handlerPool.get(loginner) != null)
			errorFlag = 2;
		else if ("all".equals(loginner))
			errorFlag = 3;
		else if (loginner == null
				|| !loginner.matches("[\\w\\u4E00-\\u9FA5_]+"))
			errorFlag = 4;
		switch (errorFlag) {
		case 0:
			System.out.println(loginner + " login");
			ChatHandler handler = new ChatHandler(loginner, socket, handlerPool);
			MsgPackage msgPackage2 = new MsgPackage(Operation.LOGIN, null,
					null, "Login suceess", null, null);
			String msgJson2 = JsonUtil.objectToString(msgPackage2);
			writer.println(msgJson2);
			handlerPool.put(loginner, handler);
			threadPool.submit(handler);
			break;
		case 1:
			// this msgPackage for send infomation to loginner
			msgPackage2 = new MsgPackage(Operation.EXIT, null, null,
					"password error", null, null);
			msgJson2 = JsonUtil.objectToString(msgPackage2);
			writer.println(msgJson2);
			break;
		case 2:
			msgPackage2 = new MsgPackage(Operation.EXIT, null, null,
					"you have login", null, null);
			msgJson2 = JsonUtil.objectToString(msgPackage2);
			writer.println(msgJson2);

			break;
		case 3:

		case 4:
			msgPackage2 = new MsgPackage(Operation.EXIT, null, null,
					"illegal name", null, null);
			msgJson2 = JsonUtil.objectToString(msgPackage2);
			writer.println(msgJson2);

			break;
		default:
			break;
		}
		if (errorFlag != 0) {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
